package com.kira.scaffoldmvc.Mapper;

import com.kira.scaffoldmvc.POJO.Book;
import com.kira.scaffoldmvc.POJO.BookIsbn;
import com.kira.scaffoldmvc.POJO.DTO.BookDTO;
import com.kira.scaffoldmvc.POJO.DTO.GetTotalDTO;
import com.kira.scaffoldmvc.POJO.Detail;
import com.kira.scaffoldmvc.POJO.RentBook;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface BookMapper {

    //创建借书订单
    @Insert("insert into rentbook (rent_id,create_time,user_id,book_id,status,anticipate,book_name)" +
            "values (#{rentId},now(),#{userId},#{bookId},0,DATE_ADD(NOW(), INTERVAL 1 MONTH),#{bookName})")
    void rentBook(String bookId, String rentId, String userId, String bookName);

    //修改书的状态为已借出
    @Update("update book set status =#{status} where book_id=#{bookId}")
    void changeBookStatus(String bookId, Integer status);

    @Select("select * from book where book_id=#{bookId}")
    Book getBookById(String bookId);

    @Update("update rentbook set status=1 where rent_id=#{rentId}")
    void backBook(String rentId);

    @Select("select * from rentbook where user_id=#{userId}")
    List<RentBook> getHistoryById(String userId);


    @Select("select type, count(*) as total from book group by type")
    List<GetTotalDTO> getTotal();

    @Select("select * from book order by hot DESC ")
    List<Book> getAllBook();

    @Insert("insert into book (book_id,publish,create_time,`type`,location,status,introduce,author,hot,book_name,isbn)values " +
            "(#{bookId},#{publish},now(),#{type},#{location},#{status},#{introduce},#{author},#{hot},#{bookName},#{isbn})")
    void InsertBook(Book book);

    @Update("update book set publish=#{book.publish},`type`=#{book.type},location=#{book.location}," +
            "status=#{book.status},introduce=#{book.introduce},author=#{book.author},hot=#{book.hot},book_name=#{book.bookName},isbn=#{book.isbn} where book_id=#{bookId}")
    void updateBook(BookDTO book, String bookId);

    @Select("select * from rentbook where status=0 AND NOW() > DATE_ADD(create_time, INTERVAL 1 MONTH) ")
    List<RentBook> check();

    @Select("select * from rentbook")
    List<RentBook> getRent();

    @Select("select book_id,book_name,publish,introduce,isbn,author from book where isbn=#{isbn}")
    Detail getByISBN(String isbn);
}
